// 请求
import axios from 'axios'
import serverIp from '../../../config/serverIp'
import qs from 'qs'
axios.defaults.baseURL = serverIp.serverArgument + serverIp.serverHost + ':' + serverIp.serverPort
axios.defaults.withCredentials = false // 解决axios post不能传参

const user = {
  state: {
    userName: 'null', // 用户名
    userListSta: [], // 用户数据列表
    userAddModalSta: false, // 新增用户模态框状态
    userRoleModalSta: false, // 角色模态框状态
    userIdSta: '', // 当前选中的用户Id
    userRoleSta: [], // 当前选中用户的角色
    userEditModalSta: false, // 编辑用户模态框
    userOneSta: {} // 当前选中的用户
  },
  mutations: {
    // 修改用户名
    userNameMut: (state, payload) => {
      state.userName = payload.name
    },
    // 修改用户数据
    userListMut: (state, payload) => {
      state.userListSta = payload
    },
    userListUpdateOneMut: (state, payload) => {
      let userId = payload.state._id.toString()
      let user = payload.state
      let userListSta = state.userListSta
      for (let i of userListSta) {
        if (i.id == userId) {
          i.id = user._id
          i.name = user.name
          i.crtime = user.crtime
          i.chtime = user.chtime
          break
        }
      }
    },
    // 删除用户列表里的一条数据
    userListDelOneMut: (state, payload) => {
      let userId = payload.state.id.toString()
      let userListSta = state.userListSta
      for (let i = 0; i < userListSta.length; i++) {
        if (userListSta[i]._id == userId) {
          userListSta.splice(i, 1)
          break
        }
      }
    },
    // 修改新增用户模态框状态
    userAddModalMut: (state, payload) => {
      state.userAddModalSta = payload.state
    },
    // 把新用户添加到列表内
    userAddMut: (state, payload) => {
      let user = payload.state
      state.userListSta.push(user)
    },
    // 修改角色模态框状态
    userRoleModalMut: (state, payload) => {
      state.userRoleModalSta = payload.state
    },
    // 修改用户角色id
    userIdMut: (state, payload) => {
      state.userIdSta = payload.state
    },
    // 修改当前用户角色
    userRoleMut: (state, payload) => {
      state.userRoleSta = payload.state
    },
    // 修改用户编辑模态框
    userEditModalMut: (state, payload) => {
      state.userEditModalSta = payload.state
    },
    // 获取当前选中的用户
    userOneMut: (state, payload) => {
      state.userOneSta = payload.state
    }

  },
  actions: {
    // 请求用户数据
    userListAct: (context) => {
      let obj = qs.stringify({})
      axios.post('/acl/userList', obj).then(res => {
        let userList = res.data
        context.commit('userListMut', userList)
      })
    }
  }
}
export default user
